{extend name="base"/}
{block name="resources"}
{/block}
{block name="main"}
<div class="layui-collapse ns-tips">
	<div class="layui-colla-item">
		<h2 class="layui-colla-title">操作提示</h2>
		<ul class="layui-colla-content layui-show">
			<li>当前页面针对续签申请进行管理，可以针对续签申请审核支付操作。</li>
			<li>续签流程：会员续签申请上传支付凭据->审核支付信息->审核通过延续入驻时间。</li>
		</ul>
	</div>
</div>

<div class="ns-screen layui-collapse" lay-filter="selection_panel">
	<div class="layui-colla-item">
		<h2 class="layui-colla-title">筛选</h2>
		{if $is_addon_city == 1}
		<form class="layui-colla-content layui-form layui-show">
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">主营行业</label>
					<div class="layui-input-inline">
						<select name="category_id" lay-filter="status">
							<option value="">全部</option>
							{foreach $shop_category_list as $list_k => $list_v}
							<option value="{$list_v.category_id}">{$list_v.category_name}</option>
							{/foreach}
						</select>
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">开店套餐</label>
					<div class="layui-input-inline">
						<select name="group_id" lay-filter="status">
							<option value="">全部</option>
							{foreach $shop_group_list as $list_k => $list_v}
							<option value="{$list_v.group_id}">{$list_v.group_name}</option>
							{/foreach}
						</select>
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">申请状态</label>
					<div class="layui-input-inline">
						<select name="apply_state" lay-filter="status">
							<option value="">全部</option>
							<option value="1">待审核</option>
							<option value="2">审核成功</option>
						</select>
					</div>
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">店铺名称</label>
					<div class="layui-input-inline">
						<input type="text" id="search_text" name="site_name" placeholder="店铺名称" autocomplete="off" class="layui-input">
					</div>
				</div>
				
				<div class="layui-inline">
					<label class="layui-form-label">城市分站：</label>
					<div class="layui-input-inline">
						<select name="website_id" lay-filter="" lay-search="">
							<option value="">请选择城市分站</option>
							{foreach $website_list as $website_k => $website_v}
							{if $website_v.site_id == 0}
							<option value="-1">无</option>
							{else /}
							<option value="{$website_v.site_id}">{$website_v.site_area_name}</option>
							{/if}
							{/foreach}
						</select>
					</div>
				</div>
			</div>
			<div class="ns-form-row">
				<button class="layui-btn ns-bg-color" lay-submit lay-filter="search_website">筛选</button>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			</div>
		</form>
		{else/}
		<form class="layui-colla-content layui-form layui-show">
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">主营行业</label>
					<div class="layui-input-inline">
						<select name="category_id" lay-filter="status">
							<option value="">全部</option>
							{foreach $shop_category_list as $list_k => $list_v}
							<option value="{$list_v.category_id}">{$list_v.category_name}</option>
							{/foreach}
						</select>
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">开店套餐</label>
					<div class="layui-input-inline">
						<select name="group_id" lay-filter="status">
							<option value="">全部</option>
							{foreach $shop_group_list as $list_k => $list_v}
							<option value="{$list_v.group_id}">{$list_v.group_name}</option>
							{/foreach}
						</select>
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">申请状态</label>
					<div class="layui-input-inline">
						<select name="apply_state" lay-filter="status">
							<option value="">全部</option>
							<option value="1">待审核</option>
							<option value="2">审核成功</option>
						</select>
					</div>
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">店铺名称</label>
					<div class="layui-input-inline ns-len-mid">
						<input type="text" id="search_text" name="site_name" placeholder="店铺名称" autocomplete="off" class="layui-input">
					</div>
				</div>
			</div>
			<div class="ns-form-row">
				<button class="layui-btn ns-bg-color" lay-submit lay-filter="search">筛选</button>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			</div>
		</form>
		{/if}
	</div>
</div>

<div class="layui-tab ns-table-tab" lay-filter="reopen_list_tab">
	<ul class="layui-tab-title">
		<li class="layui-this" data-status="">全部</li>
		<li data-status="1" data-type="apply_state">待审核</li>
		<li data-status="2" data-type="apply_state">审核通过</li>
		<li data-status="-1" data-type="apply_state">审核拒绝</li>
	</ul>
	<div class="layui-tab-content">
		{if $is_addon_city == 1}
		<table id="renewal_website_list" lay-filter="renewal_website_list"></table>
		{else/}
		<table id="renewal_list" lay-filter="renewal_list"></table>
		{/if}
	</div>
</div>

<input type="hidden" id="is_addon_city" value="{$is_addon_city}" />

<!-- 付款凭证 -->
<script type="text/html" id="paymentVoucher">
	<div class="ns-img-box">
		{{#  if(d.paying_money_certificate){  }}
		<img onclick="showImg('{{ ns.img(d.paying_money_certificate) }}')" src="{{ns.img(d.paying_money_certificate)}}"/>
		{{#  }  }}
	</div>
</script>

<!-- 状态 -->
<script type="text/html" id="status">
	{{#  if(d.apply_state == 1){  }}
	待审核
	{{#  }else if(d.apply_state == -1){  }}
	审核拒绝
	{{#  }else if(d.apply_state == 2){  }}
	审核成功
	{{#  }  }}
</script>

<!-- 工具栏操作 -->
<script type="text/html" id="action">
	<div class="ns-table-btn">
		{{#  if(d.apply_state == 1){  }}
		<a class="layui-btn" lay-event="pass">通过续签</a>
		<a class="layui-btn" lay-event="refuse">拒绝续签</a>
		<a class="layui-btn" lay-event="detail">申请详情</a>
		{{#  }else if(d.apply_state == -1){  }}
		<a class="layui-btn" lay-event="detail">申请详情</a>
		{{#  }else if(d.apply_state == 2){  }}
		<a class="layui-btn" lay-event="detail">申请详情</a>
		{{#  }  }}
	</div>
</script>
{/block}
{block name="script"}
<script>
	layui.use(['form', 'element'], function() {
		var table, table_website,
			form = layui.form,
            element = layui.element,
			repeat_flag = false;
		form.render();

        //监听Tab切换，以改变地址hash值
        element.on('tab(reopen_list_tab)', function () {
			var is_addon_city = $("#is_addon_city").val();
			if (is_addon_city == 1) {
				table_website.reload({
					page: {
						curr: 1
					},
					where: {
						'apply_state': this.getAttribute('data-status')
					}
				});
			} else {
				table.reload({
					page: {
						curr: 1
					},
					where: {
						'apply_state': this.getAttribute('data-status')
					}
				});
			}
        });

		table = new Table({
			elem: '#renewal_list',
			url: ns.url("admin/shopreopen/reopen"),
			cols: [
				[{
                    field: 'reopen_no',
                    title: '账单编号',
                    unresize: 'false',
					width: '15%'
                },{
					field: 'site_name',
					title: '店铺名称',
					unresize: 'false',
					width: '15%'
				}, {
					field: 'shop_group_name',
					title: '续签等级',
					unresize: 'false',
					width: '10%'
				}, {
					field: 'apply_year',
					title: '续签时长（年）',
					unresize: 'false',
					width: '10%'
				}, {
					field: 'paying_amount',
					title: '<span style="padding-right: 15px;">续签费用</span>',
					unresize: 'false',
					align: 'right',
					width: '10%',
					templet: function(data) {
						return '<span style="padding-right: 15px;">￥'+ data.paying_amount +'</span>';
					}
				}, {
					title: '状态',
					unresize: 'false',
					templet: '#status',
					width: '10%'
				}, {
					field: 'create_time',
					title: '创建时间',
					unresize: 'false',
					width: '15%',
					templet: function(data) {
						return ns.time_to_date(data.create_time)
					}
				}, {
					title: '操作',
					width: '15%',
					unresize: 'false',
					toolbar: '#action'
				}]
			]
		});
		
		// 有城市分站
		table_website = new Table({
			elem: '#renewal_website_list',
			url: ns.url("city://admin/shop/reopen"),
			cols: [
				[{
		            field: 'reopen_no',
		            title: '账单编号',
		            unresize: 'false',
					width: '13%'
		        },{
					field: 'site_name',
					title: '店铺名称',
					unresize: 'false',
					width: '13%'
				}, {
					field: 'shop_group_name',
					title: '续签等级',
					unresize: 'false',
					width: '8%'
				}, {
					width: '8%',
					field: 'site_area_name',
					title: '城市分站',
					unresize: 'false',
					templet: function(data) {
						return data.site_area_name == '全国' ? '--' : data.site_area_name;
					},
				}, {
					field: 'apply_year',
					title: '续签时长（年）',
					unresize: 'false',
					width: '10%'
				}, {
					field: 'paying_amount',
					title: '<span style="padding-right: 15px;">续签费用</span>',
					unresize: 'false',
					align: 'right',
					width: '10%',
					templet: function(data) {
						return '<span style="padding-right: 15px;">￥'+ data.paying_amount +'</span>';
					}
				}, {
					title: '状态',
					unresize: 'false',
					templet: '#status',
					width: '8%'
				}, {
					field: 'create_time',
					title: '创建时间',
					unresize: 'false',
					width: '15%',
					templet: function(data) {
						return ns.time_to_date(data.create_time)
					}
				}, {
					title: '操作',
					width: '15%',
					unresize: 'false',
					toolbar: '#action'
				}]
			]
		});
		
		/**
		 * 监听工具栏操作
		 */
		table.tool(function(obj) {
			var data = obj.data;
			switch (obj.event) {
				case 'pass': //通过
					passApply(data.id, data.site_id);
					break;
                case 'refuse': //拒绝
                    refuseApply(data.id);
                    break;
				case 'detail': //详情
					location.href = ns.url("admin/shopreopen/reopendetail?id="+ data.id);
					break;
			}
		});
		
		// 有城市分站
		table_website.tool(function(obj) {
			var data = obj.data;
			switch (obj.event) {
				case 'pass': //通过
					passApply(data.id, data.site_id);
					break;
		        case 'refuse': //拒绝
		            refuseApply(data.id);
		            break;
				case 'detail': //详情
					location.href = ns.url("admin/shopreopen/reopendetail?id="+ data.id);
					break;
			}
		});

		//通过申请
		function passApply(id, site_id) {
			if (repeat_flag) return false;
			repeat_flag = true;
			layer.confirm('确定要通过续签申请吗?', function() {
				$.ajax({
					url: ns.url("admin/shopreopen/pass"),
					data: {
						"id": id,
						"site_id": site_id
					},
					dataType: 'JSON',
					type: 'POST',
					success: function(res) {
						layer.msg(res.message);
						repeat_flag = false;
						
						if (res.code == 0) {
							var is_addon_city = $("#is_addon_city").val();
							if (is_addon_city == 1) {
								table_website.reload();
							} else {
								table.reload();
							}
						}
					}
				});
			}, function () {
				layer.close();
				repeat_flag = false;
			});
		}

        //拒绝申请
        function refuseApply(id) {
            if (repeat_flag) return false;
            repeat_flag = true;
            layer.confirm('确定要拒绝续签申请吗?', function() {
                $.ajax({
                    url: ns.url("admin/shopreopen/fail"),
                    data: {
                        "id": id,
                    },
                    dataType: 'JSON',
                    type: 'POST',
                    success: function(res) {
                        layer.msg(res.message);
                        repeat_flag = false;

                        if (res.code == 0) {
                            var is_addon_city = $("#is_addon_city").val();
                            if (is_addon_city == 1) {
                            	table_website.reload();
                            } else {
                            	table.reload();
                            }
                        }
                    }
                });
            }, function () {
                layer.close();
                repeat_flag = false;
            });
        }
		
		/**
		 * 搜索功能
		 */
		form.on('submit(search)', function(data) {
			table.reload({
				page: {
					curr: 1
				},
				where: data.field
			});
			return false;
		});
		
		// 有城市分站
		form.on('submit(search_website)', function(data) {
			table_website.reload({
				page: {
					curr: 1
				},
				where: data.field
			});
			return false;
		});
	});

	function showImg(img) {
		layer.open({
			type: 21,
			title: false,
			closeBtn: 0,
			skin: 'layui-layer-nobg', //没有背景色
			shadeClose: true,
			content: '<img src="'+img+'">'
		});
	}
</script>
{/block}